global projectdir "~"
global datadir "$projectdir/data"




cd $datadir/raw_pulls/lehd_usiris
use ecf_interleave_seinunit_t13_2018_usiris_2018q4, clear
keep sein seinunit qtime naics2017fnl naics2012fnl naics2007fnl

local years 2007 2012 2017
foreach year in `years' {

local levels "seinunit_any"
foreach level in `levels' {

gen naics4_`level' = regexs(1) if regexm(naics2017fnl, "^([0-9][0-9][0-9][0-9])")

gen ht_`level'_`year' = 0
replace ht_`level'_`year' = 1 if naics4_`level' == "1131"
replace ht_`level'_`year' = 1 if naics4_`level' == "1132"
replace ht_`level'_`year' = 1 if naics4_`level' == "2111"
replace ht_`level'_`year' = 1 if naics4_`level' == "2211"
replace ht_`level'_`year' = 1 if naics4_`level' == "3241"
replace ht_`level'_`year' = 1 if naics4_`level' == "3251"
replace ht_`level'_`year' = 1 if naics4_`level' == "3252"
replace ht_`level'_`year' = 1 if naics4_`level' == "3253"
replace ht_`level'_`year' = 1 if naics4_`level' == "3254"
replace ht_`level'_`year' = 1 if naics4_`level' == "3255"
replace ht_`level'_`year' = 1 if naics4_`level' == "3259"
replace ht_`level'_`year' = 1 if naics4_`level' == "3332"
replace ht_`level'_`year' = 1 if naics4_`level' == "3336"
replace ht_`level'_`year' = 1 if naics4_`level' == "3339"
replace ht_`level'_`year' = 1 if naics4_`level' == "3341"
replace ht_`level'_`year' = 1 if naics4_`level' == "3342"
replace ht_`level'_`year' = 1 if naics4_`level' == "3343"
replace ht_`level'_`year' = 1 if naics4_`level' == "3344"
replace ht_`level'_`year' = 1 if naics4_`level' == "3345"
replace ht_`level'_`year' = 1 if naics4_`level' == "3346"
replace ht_`level'_`year' = 1 if naics4_`level' == "3353"
replace ht_`level'_`year' = 1 if naics4_`level' == "3364"
replace ht_`level'_`year' = 1 if naics4_`level' == "3369"
replace ht_`level'_`year' = 1 if naics4_`level' == "4324"
replace ht_`level'_`year' = 1 if naics4_`level' == "4861"
replace ht_`level'_`year' = 1 if naics4_`level' == "4862"
replace ht_`level'_`year' = 1 if naics4_`level' == "4869"
replace ht_`level'_`year' = 1 if naics4_`level' == "5112"
replace ht_`level'_`year' = 1 if naics4_`level' == "5161"
replace ht_`level'_`year' = 1 if naics4_`level' == "5171"
replace ht_`level'_`year' = 1 if naics4_`level' == "5172"
replace ht_`level'_`year' = 1 if naics4_`level' == "5173"
replace ht_`level'_`year' = 1 if naics4_`level' == "5174"
replace ht_`level'_`year' = 1 if naics4_`level' == "5179"
replace ht_`level'_`year' = 1 if naics4_`level' == "5181"
replace ht_`level'_`year' = 1 if naics4_`level' == "5182"
replace ht_`level'_`year' = 1 if naics4_`level' == "5211"
replace ht_`level'_`year' = 1 if naics4_`level' == "5232"
replace ht_`level'_`year' = 1 if naics4_`level' == "5413"
replace ht_`level'_`year' = 1 if naics4_`level' == "5415"
replace ht_`level'_`year' = 1 if naics4_`level' == "5416"
replace ht_`level'_`year' = 1 if naics4_`level' == "5417"
replace ht_`level'_`year' = 1 if naics4_`level' == "5511"
replace ht_`level'_`year' = 1 if naics4_`level' == "5612"
replace ht_`level'_`year' = 1 if naics4_`level' == "8112"

}

drop naics4_seinunit_any
}


gen seinunit_any_count = 1

gen ht_seinunit_any_2007_count = ht_seinunit_any_2007
gen ht_seinunit_any_2012_count = ht_seinunit_any_2012
gen ht_seinunit_any_2017_count = ht_seinunit_any_2017

collapse (sum) *_count, by(sein qtime) fast

compress
cd $datadir/intermediate_files
save lehd_sein_any_ht, replace








cd $datadir/raw_pulls/lehd_usiris
use ecf_interleave_seinunit_t13_2018_usiris_2018q4, clear
keep sein naics2017fnl naics2012fnl naics2007fnl
duplicates drop

local years 2007 2012 2017
foreach year in `years' {


local levels "seinunit_any_ever"
foreach level in `levels' {

gen naics4_`level' = regexs(1) if regexm(naics2017fnl, "^([0-9][0-9][0-9][0-9])")

gen ht_`level'_`year' = 0
replace ht_`level'_`year' = 1 if naics4_`level' == "1131"
replace ht_`level'_`year' = 1 if naics4_`level' == "1132"
replace ht_`level'_`year' = 1 if naics4_`level' == "2111"
replace ht_`level'_`year' = 1 if naics4_`level' == "2211"
replace ht_`level'_`year' = 1 if naics4_`level' == "3241"
replace ht_`level'_`year' = 1 if naics4_`level' == "3251"
replace ht_`level'_`year' = 1 if naics4_`level' == "3252"
replace ht_`level'_`year' = 1 if naics4_`level' == "3253"
replace ht_`level'_`year' = 1 if naics4_`level' == "3254"
replace ht_`level'_`year' = 1 if naics4_`level' == "3255"
replace ht_`level'_`year' = 1 if naics4_`level' == "3259"
replace ht_`level'_`year' = 1 if naics4_`level' == "3332"
replace ht_`level'_`year' = 1 if naics4_`level' == "3336"
replace ht_`level'_`year' = 1 if naics4_`level' == "3339"
replace ht_`level'_`year' = 1 if naics4_`level' == "3341"
replace ht_`level'_`year' = 1 if naics4_`level' == "3342"
replace ht_`level'_`year' = 1 if naics4_`level' == "3343"
replace ht_`level'_`year' = 1 if naics4_`level' == "3344"
replace ht_`level'_`year' = 1 if naics4_`level' == "3345"
replace ht_`level'_`year' = 1 if naics4_`level' == "3346"
replace ht_`level'_`year' = 1 if naics4_`level' == "3353"
replace ht_`level'_`year' = 1 if naics4_`level' == "3364"
replace ht_`level'_`year' = 1 if naics4_`level' == "3369"
replace ht_`level'_`year' = 1 if naics4_`level' == "4324"
replace ht_`level'_`year' = 1 if naics4_`level' == "4861"
replace ht_`level'_`year' = 1 if naics4_`level' == "4862"
replace ht_`level'_`year' = 1 if naics4_`level' == "4869"
replace ht_`level'_`year' = 1 if naics4_`level' == "5112"
replace ht_`level'_`year' = 1 if naics4_`level' == "5161"
replace ht_`level'_`year' = 1 if naics4_`level' == "5171"
replace ht_`level'_`year' = 1 if naics4_`level' == "5172"
replace ht_`level'_`year' = 1 if naics4_`level' == "5173"
replace ht_`level'_`year' = 1 if naics4_`level' == "5174"
replace ht_`level'_`year' = 1 if naics4_`level' == "5179"
replace ht_`level'_`year' = 1 if naics4_`level' == "5181"
replace ht_`level'_`year' = 1 if naics4_`level' == "5182"
replace ht_`level'_`year' = 1 if naics4_`level' == "5211"
replace ht_`level'_`year' = 1 if naics4_`level' == "5232"
replace ht_`level'_`year' = 1 if naics4_`level' == "5413"
replace ht_`level'_`year' = 1 if naics4_`level' == "5415"
replace ht_`level'_`year' = 1 if naics4_`level' == "5416"
replace ht_`level'_`year' = 1 if naics4_`level' == "5417"
replace ht_`level'_`year' = 1 if naics4_`level' == "5511"
replace ht_`level'_`year' = 1 if naics4_`level' == "5612"
replace ht_`level'_`year' = 1 if naics4_`level' == "8112"

}

drop naics4_seinunit_any_ever
}


gen seinunit_any_ever_count = 1

gen ht_seinunit_any_ever_2007_count = ht_seinunit_any_ever_2007
gen ht_seinunit_any_ever_2012_count = ht_seinunit_any_ever_2012
gen ht_seinunit_any_ever_2017_count = ht_seinunit_any_ever_2017

collapse (sum) *_count, by(sein) fast

cd $datadir/intermediate_files
merge 1:m sein using lehd_sein_any_ht
drop _merge

compress
cd $datadir/intermediate_files
save lehd_sein_any_ht, replace



